import Api from '../../constants/Api'

export default {
  namespaced: true,
  state: {
    logList: []
  },
  mutations: {
    logListTo(state, data) {
      state.logList = data
    }
  },
  actions: {
    getLogList({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.getLogList,
        data: {
          current: that.current,
          pageSize: that.pageSize,
          optEndDate: that.optEndDate,
          optModel: that.optModel,
          optStartDate: that.optStartDate,
          optType: that.optType,
          optUser: that.optUser
        }
      }).then(res => {
        if (res.code === 20001) {
          commit('logListTo', res.data.records)
          that.total = res.data.total
          that.tableLoading = false
        } else {
          that.tableLoading = false
        }
      })
    },
    getSettingData({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.getSettingData,
        data: {}
      }).then(res => {
        that.$nextTick(() => {
          that.settingForm = res.data
        })
      })
    },
    updateSettings({commit}, that) {
      let _data = {}
      for (let item in that.settingForm) {
        if (that.settingForm[item].settingType === 'T') {
          _data[item] = that.settingForm[item].value
        }
        if (that.settingForm[item].settingType === 'S') {
          _data[item] = that.settingForm[item].currentValueId
        }
        if (that.settingForm[item].settingType === 'C') {
          _data[item] = that.settingForm[item].currentValueIds
        }
        if (that.settingForm[item].settingType === 'R') {
          let start = {'startId': that.settingForm[item].startValueId, 'startValue': that.settingForm[item].startValue}
          let end = {'endId': that.settingForm[item].endValueId, 'endValue': that.settingForm[item].endValue}
          _data[item] = {'start': start, 'end': end}
        }
      }
      that.$axios({
        method: 'post',
        url: Api.updateSettings,
        data: _data
      }).then(res => {
        if (res.code === 20003) {
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    }
  }
}
